﻿<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>MANO年会抽奖</title>

<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="js/myalert.js" charset="utf-8"></script>

<style>
*{margin:0;padding:0;}
img{display:block;}
i{font-style:normal;}
.vetically{justify-content:center;align-items:center;display:-webkit-flex;}
.prize_con{position: absolute;width: 100%;height: 100%;background: url(images/firstp_bg.jpg) no-repeat left top / 100% 100%;overflow: hidden;}
.prize_grade{font-size:98px;color: #ffe9af;text-align: center;margin: 60px auto 0;}
.prize_list{width:55%;height:230px;margin: 20px auto 55px;text-align: center;overflow: hidden;}
.prize_list ul{width:100%;font-size:0;}
.prize_list li{display:inline-block;font-size:45px;color:#f1bf90;text-align: center;width:20%;line-height:100px;font-family:Arial;}
.start{width: 250px;height: 90px;margin:0 auto;cursor:pointer;}
.prize_set{position: absolute;right: 60px;bottom:140px;font-size: 16px;color: #f7f3e8;line-height: 30px;}
.prize_set li{display: inline-block;margin-left: 20px;}
.set_grade select,.set_people input, .set_money input{background: #fff;width:110px;height:36px;border:1px solid #8f0000;margin-left: .1rem;color: #000;padding-left:10px;}
</style>

</head>
<body>

<div class="wrap">
  <div class="prize_con">
      <p class="prize_grade"><span>开始抽奖</span> <i>0</i>人</p>
        <div class="prize_list vetically">
          <ul id="prizeList">
            
          </ul>
        </div>
      <p class="start"><img src="images/prize_start.png" alt=""></p>
      <ul class="prize_set">
          <li class="set_grade">奖等
            <select id="set_grade">
              <option>选择奖等</option>
              <option>特等奖</option>
              <option>一等奖</option>
              <option>二等奖</option>
              <option>三等奖</option>
            </select>
          </li>
          <li class="set_people">人数<input type="tel" placeholder="输入中奖人数" id="prizeCount"></li>
          
      </ul>
  </div>
</div>
<input type="hidden" value="0" id="prize_btn">
<script>
  var myNumber;
  var allNum,tempI=0;
  var arr = [];
  var code = [];
  
  var prizeResult;
  
  var temp = [];
  
  var tempCode = [];
  
  function init(peopleCount){
    for(var i = 0; i < peopleCount; i++){
	  if(i<9){
	     code[i] = "0" + (i+1);
	  }else{
	     code[i] = (i+1)+"";
	  }  
    }
  }
  
  
	
	function getQueryString(name){
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
   }

	
	/*随机所有的code并且不重复*/
  function initNum(num) {
    var li = "";
    for(var i = 0; i < num; i++){
      li += '<li>00</li>';
    }
    $(".prize_list ul").html(li);	
  }

  /*随机所有的code并且不重复*/
  function showRandomNum(num) {
    var li = "";
	
    for(var i = 0; i < code.length; i++){
      arr[i] = i;
    }
    arr.sort(function(){
      return 0.5 - Math.random();
    });

    for(var i = 0; i < 1; i++){
      var index = arr[i];
      li += '<li>'+code[index]+'</li>';
	  temp[i] = code[index];
    }
    $("#prizeList li").eq(num).text(code[index]);	
  }
  
   function cssAble(){
      $("#set_grade").removeAttr("disabled");
	  $("#prizeCount").removeAttr("disabled");
   }
   
   function cssDisable(){
      $("#set_grade").attr("disabled",true);
	  $("#prizeCount").attr("disabled",true);
	  
   }

  $(function () {
  
    var peopleCount = getQueryString("count");
	init(peopleCount);
  
    $(".start").click(function(){
      if($("#prize_btn").val() == 0){        
        if($("#set_grade").val() == "选择奖等") {
            alert("请选择奖等");
            return;
        }else if($("#prizeCount").val() == "") {
            alert("请输入中奖人数");
            return;
        }else if($("#prizeCount").val() > 10) {
            alert("单次抽奖人数不能超过10人");
            return;
        }else if($("#prizeMoney").val() == "") {
            alert("请输入中奖金额");
            return;
        }else{
          $("#prize_btn").val(1);
		  if(tempI<allNum){
		     myNumber = setInterval(function(){
            showRandomNum(tempI);
          }, 10);
		  $(this).find("img").attr("src","images/prize_stop.png");
		  cssDisable();
		  }else{
			tempI=0;
			allNum=0;
			$("#prize_btn").val(0);
			cssAble();
            return;
		  }        
        }        
      }else{
        $("#prize_btn").val(0);
        clearInterval(myNumber);
        $(this).find("img").attr("src","images/prize_start.png");
		tempI++;
      } 

	  console.log(temp);
	  
	  //排除重复的数字
	var tempCode = [];
	for(var i = 0,j=0; i < code.length; i++){
       if(temp.indexOf(code[i])<0){
	       tempCode[j++] = code[i];
	   }
    }
	
	temp=[];
	arr = [];
	
	code = tempCode;
    console.log(code);	
    });
	
    //回车键控制开始和停止
    $(document).keydown(function (event) {
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if (e && e.keyCode == 13) { // enter 键
            $(".start").click();
        }
    });

    $("#set_grade").change(function(){
      $(".prize_grade span").text($(this).val());
	  $("#prize_btn").val(0);
    });

    $("#prizeCount").keyup(function(){
      $(".prize_grade i").text($(this).val());
	  initNum($(this).val());
	  allNum = $(this).val();
	  console.log(allNum);
	  console.log(tempI);
    });
}); 
</script>

</body>
</html>
